#include "hot.hpp"
#include "server.hpp"
#include <thread>

void FileUtilTest()
{
  //在cloud作用域内的FileUtil工具类中实例化一个匿名对象，调用成员接口来进行目录创建
  //cloud::FileUtil("./testdir/adir").CreateDirectory();
  //在cloud作用域内的FileUtil工具类中创建文件
  //cloud::FileUtil("./testdir/a.txt").Write("hello world\n");
  //从文件中读取内容到body中
  //std::string body;
  //cloud::FileUtil("./testdir/a.txt").Read(&body);
  //std::cout << body << std::endl;
  //std::cout << cloud::FileUtil("./testdir/a.txt").Size() << std::endl;//大小
  //std::cout << cloud::FileUtil("./testdir/a.txt").MTime() << std::endl;//最后一次修改时间
  //std::cout << cloud::FileUtil("./testdir/a.txt").ATime() << std::endl;//最后一次访问时间
  std::vector<std::string> arry;
  cloud::FileUtil("./testdir").ScanDirectory(&arry);
  for(auto & a : arry)
  {
    std::cout << a << std::endl;
  }  
}

void JsonTest()
{
  Json::Value val;//序列化测试
  val["姓名"] = "小明";
  val["性别"] = "男";
  val["年龄"] = 18;
  val["成绩"].append(77.5);
  val["成绩"].append(78.5);
  val["成绩"].append(79.5);
  std::string body;
  cloud::JsonUtil::Serialize(val, &body);
  std::cout << body << std::endl;

  Json::Value root;//反序列化测试
  cloud::JsonUtil::UnSerialize(body, &root);//将body反序列化得到的数据放到root中
  std::cout << root["姓名"].asString() << std::endl;
  std::cout << root["性别"].asString() << std::endl;
  std::cout << root["年龄"].asInt() << std::endl;
  std::cout << root["成绩"][0].asFloat() << std::endl;
  std::cout << root["成绩"][1].asFloat() << std::endl;
  std::cout << root["成绩"][2].asFloat() << std::endl;
}

void CompressTest()
{
  cloud::FileUtil("./hello.txt").Compress("hello.zip");
  cloud::FileUtil("./hello.zip").UnCompress("bit.txt");
}

void DataTest()
{
  cloud::DataManager data;
  
  std::vector<cloud::FileInfo> arry;
  data.SelectAll (&arry);
  for (auto &e : arry)
  {
    std::cout << e.filename  << std::endl;
    std::cout << e.url_path  <<  std::endl;
    std::cout << e.real_path << std::endl;
    std::cout << e.file_size << std::endl;
    std::cout << e.back_time << std::endl;
    std::cout << e.pack_flag << std::endl;
    std::cout << e.pack_path << std::endl;
  }

  /* 
  data.Insert("./backup_dir/hello.txt");
  data.UpdataStatus("./backup_dir/hello.txt", true);
  std::vector<cloud::FileInfo> arry;
  data.SelectAll(&arry);
  for(auto &e : arry)
  {
    std::cout << e.filename << std::endl;
    std::cout << e.url_path << std::endl;
    std::cout << e.real_path << std::endl;
    std::cout << e.file_size << std::endl;
    std::cout << e.back_time << std::endl;
    std::cout << e.pack_flag << std::endl;
    std::cout << e.pack_path <<std::endl;
  }
  std::cout << "-----------------------------\n";
  data.UpdataStatus("./backup_dir/hello.txt", false);
  cloud::FileInfo e;
  data.SelectOne("/download/hello.txt", &e);
    std::cout << e.filename << std::endl;
    std::cout << e.url_path << std::endl;
    std::cout << e.real_path << std::endl;
    std::cout << e.file_size << std::endl;
    std::cout << e.back_time << std::endl;
    std::cout << e.pack_flag << std::endl;
    std::cout << e.pack_path << std::endl;
  

  std::cout << "--------------delete--------------\n";
  data.DeleteOne("/download/hello.txt");//针对文件名进行删除
  arry.clear();
  data.SelectAll(&arry);
  for(auto &e : arry)
  {
    std::cout << e.filename << std::endl;
    std::cout << e.url_path << std::endl;
    std::cout << e.real_path << std::endl;
    std::cout << e.file_size << std::endl;
    std::cout << e.back_time <<std::endl;
    std::cout << e.pack_flag <<std::endl;
    std::cout << e.pack_path <<std::endl;
  }
  */
}

cloud::DataManager *_data;
void HotTest()
{
  _data = new cloud::DataManager();
  cloud::HotManager cloud;
  cloud.RunModule();

}

void ServerTest()
{
  cloud::Server  srv;
  srv.RunModule();
}
int main()
{
  _data = new cloud::DataManager();//数据管理模块                                             
  //FileUtilTest();
  //JsonTest();
  //CompressTest();
  //DataTest();
  //HotTest();
  //ServerTest();
  std::thread hot_thread(HotTest);//热点管理模块
  std::thread srv_thread(ServerTest);//服务器业务处理模块网络通信模块

  hot_thread.join();//等待
  srv_thread.join();//等待
  return 0;
}
